<?php
class CommonTag extends Tag{
    /**
     * 标签声明
     * @var array
     */
    public $Tag = array(
        'navv' => array('block' => 1,'level'=>4),
        'arclist' => array('block' => 1,'level'=>4),
        'channel' => array('block' => 1,'level'=>4),
        'tag' => array('block' => 1,'level'=>4),
        'flink' => array('block' => 1,'level'=>4),
    );
	
	public function _tag($attr, $content, &$hd){
		$rows = isset($attr['rows']) ? $attr['rows'] : 1000;
		$str = <<<str
		<?php
			\$tagData = K('Tag')->limit($rows)->all();
			foreach(\$tagData as \$field):
			\$field['url'] = U('List/index',array('tid'=>\$field['tid']));
		?>
		{$content}
		<?php
			endforeach;
		?>
str;
		return $str;
	}
	/**
	 * 通过cid显示分类名
	 */
	public function _channel($attr, $content, &$hd){
		$rows = isset($attr['rows']) ? $attr['rows'] : 1000;
		$where = '';
		//如果在列表页
		if($cid = Q('get.cid')){
			$where .= "cid={$cid}";
		}
		//组合要返回页面的字符串
		$str = <<<str
<?php
	\$cate = K('Category')->where("$where")->limit($rows)->all();
	foreach(\$cate as \$field):
	\$field['url'] = getCateUrl(\$field['is_listhtml'],\$field['htmldir'],\$field['cid']);
?>
	{$content}
<?php
	endforeach;
?>
str;
		return $str;
		
	}

	public function _arclist($attr, $content, &$hd){
		$rows = isset($attr['rows']) ? $attr['rows'] : 1000;
		$arcAttr = isset($attr['arcAttr']) ? $attr['arcAttr'] : NULL;
		$order = isset($attr['order']) ? $attr['order'] : 'sendtime DESC';
		$pageRows = isset($attr['pageRows']) ? $attr['pageRows'] : 0;
		$pageNum = isset($attr['pageNum']) ? $attr['pageNum'] : 5;
		
		//where条件
		$where = '';
		//如果用户有传递属性
		if($arcAttr){
			$attrArr = explode(',', $arcAttr);
			foreach ($attrArr as $v) {
				//组合where
				$where .= "attr LIKE '%{$v}%' OR ";
			}
			$where = rtrim($where,'OR ');
		}
		//如果是列表页
		if($cid = Q('get.cid',0,'intval')){
			//获得所有子集cid
			$cateModel = K('Category');
			$sonCid = $cateModel->getSon($cateModel->all(),$cid);
			//把数组变为字符串
			if(empty($sonCid)){
				$sonCid = $cid;
			}else{
				$sonCid[] = $cid;
				$sonCid = implode(',', $sonCid);
			}
			$where .= "AND category_cid in({$sonCid})";
		}
		//如果点击了标签
		if($tid = Q('get.tid',0,'intval')){
			//根据标签id找到对应的文章id
			$aid = K('ArcTag')->where("tag_tid={$tid}")->getField('article_aid',true);
			$aid = implode(',', $aid);
			if(!$aid) $aid = 0;
			$where .= "AND aid in($aid)";
		}
		//去除左边的and
		$where = ltrim($where,'AND');
		
		$str = <<<str
<?php
	//如果有此参数，就是要实现分页效果
	if($pageRows){
		//总条数
		\$total = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->count();
		\$pageObj = new Page(\$total,$pageRows,$pageNum);
		\$page = \$pageObj->show();
		\$arcData = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->limit(\$pageObj->limit())->order('$order')->all();
	}else{
		\$arcData = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->limit({$rows})->order('$order')->all();
	}
	foreach(\$arcData as \$field):
	//评论总条数
	\$field['total'] = K('Comment')->where("article_aid=" . \$field['aid'])->count();
	//文章地址
	\$field['url'] = getArcUrl(\$field['is_archtml'],\$field['htmldir'],\$field['aid']);
	//文章缩略图
	\$field['thumb'] = "__ROOT__/" . \$field['thumb'];
?>
{$content}
<?php
	endforeach
?>
str;
		return $str;
	}

    /**
     * 栏目标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _navv($attr, $content, &$hd){
    	//获得多少条
    	$rows = isset($attr['rows']) ? $attr['rows'] : 1000;
		//组合要返回页面的字符串
		$str = <<<str
<?php
	\$topCate = K('Category')->where("pid=0")->limit($rows)->all();
	foreach(\$topCate as \$field):
	\$field['url'] = getCateUrl(\$field['is_listhtml'],\$field['htmldir'],\$field['cid']);
?>
	{$content}
<?php
	endforeach;
?>
str;
		return $str;
    }
	public function _flink($attr, $content, &$hd){
    	//获得多少条
    	$rows = isset($attr['rows']) ? $attr['rows'] : 1000;
		//组合要返回页面的字符串
		$str = <<<str
<?php
	\$topCate = K('Flink')->where("is_show=1")->limit($rows)->all();
	foreach(\$topCate as \$field):
?>
	{$content}
<?php
	endforeach;
?>
str;
		return $str;
    }
}











